package com.example.frame.ZFH;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import com.example.frame.R;

import org.litepal.LitePal;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class Zhangfenghua extends AppCompatActivity {
    @BindView(R.id.ed_title)
    EditText edTitle;
    @BindView(R.id.ed_opinion)
    EditText edOpinion;
    @BindView(R.id.submission)
    Button submission;
    @BindView(R.id.bu_delete)
    Button buDelete;
    @BindView(R.id.tv_me_opinion)
    TextView tvMeOpinion;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zhangfenghua);
        ButterKnife.bind(this);
    }

    private void initview() {
        if (edTitle.getText().toString().equals("")){
            Toast.makeText(this, "请输入标题", Toast.LENGTH_SHORT).show();
        }else if (edOpinion.getText().toString().equals("")){
            Toast.makeText(this, "请输入内容", Toast.LENGTH_SHORT).show();
        }else{
            LitePal.getDatabase();
            Book book = new Book();
            SimpleDateFormat simpleDateFormat=new SimpleDateFormat("yyyy.MM.dd MM:mm");
            Date date=new Date(System.currentTimeMillis());
            book.setTitle(edTitle.getText().toString().trim());
            book.setOpinion(edOpinion.getText().toString().trim());
            book.setTime(simpleDateFormat.format(date));
            book.save();
            Toast.makeText(this, "提交成功", Toast.LENGTH_SHORT).show();
        }
    }
    private void initData() {
        List<String> opinions1 = new ArrayList<String>();
        List<String> opinions2 = new ArrayList<String>();
        List<String> opinions3 = new ArrayList<String>();
        List<Book> all = LitePal.findAll(Book.class);
        Log.i("TAG", "initview1" + all.size());
        for (Book book1 : all) {
            opinions1.add(book1.getTitle());
            opinions2.add(book1.getOpinion());
            opinions3.add(book1.getTime());
            Log.i("TAG", "initview1" + book1.getTitle());
            Log.i("TAG", "initview1" + book1.getOpinion());
            Log.i("TAG", "initview丨" + book1.getTime());
        }
        //设置弹窗
        AlertDialog.Builder Dialog = new AlertDialog.Builder(this);
        final View dialogView = LayoutInflater.from(this).inflate(R.layout.activity_list, null);
        ViewHolder viewHolder = new ViewHolder(dialogView);
        Dialog.setView(dialogView);
        Dialog.setCancelable(true);
        final AlertDialog customAlert = Dialog.show();
        viewHolder.buCancal.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                customAlert.dismiss();
            }
        });
        //设置自定义逻辑
        viewHolder.opinionLv.setAdapter(new myAdapter(opinions1,opinions2,opinions3) {
        });
    }

    public class myAdapter extends BaseAdapter {
        List<String> opinions1;
        List<String> opinions2;
        List<String> opinions3;

        public myAdapter(List<String> opinions1,List<String> opinions2,List<String> opinions3) {
            this.opinions1=opinions1;
            this.opinions2=opinions2;
            this.opinions3=opinions3;
        }

        @Override
        public int getCount() {
            return opinions1.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

        @Override
        public long getItemId(int i) {
            return 0;
        }

        @Override
        public View getView(int i, View view, ViewGroup viewGroup) {
            View view1 = View.inflate(Zhangfenghua.this, R.layout.activity_list_one, null);
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy.MM.dd HH:mm");
            Date date = new Date(System.currentTimeMillis());
            ViewHolder2 viewHolder2 = new ViewHolder2(view1);
            viewHolder2.tvTitleOne.setText("标题:" + opinions1.get(i));
            viewHolder2.tvContentOne.setText("内容:" + opinions2.get(i));
            viewHolder2.tvTimeOne.setText("提交时间:" + opinions3.get(i));
            return view1;
        }
        class ViewHolder2 {
            @BindView(R.id.tv_title_one)
            TextView tvTitleOne;
            @BindView(R.id.tv_time_one)
            TextView tvTimeOne;
            @BindView(R.id.tv_content_one)
            TextView tvContentOne;
            ViewHolder2(View view) {
                ButterKnife.bind(this, view);
            }
        }
    }
    @OnClick(R.id.submission)
    public void onViewClicked() {
        initview();
        edTitle.setText("");
        edOpinion.setText("");
    }

    @OnClick(R.id.bu_delete)
    public void onViewClicked1() {
        LitePal.deleteAll(Book.class);
    }

    @OnClick(R.id.tv_me_opinion)
    public void onViewClicked2() {
        initData();
    }

    static class ViewHolder {
        @BindView(R.id.opinion_lv)
        ListView opinionLv;
        @BindView(R.id.bu_cancal)
        Button buCancal;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
